<?php

namespace app\admin\controller;

use think\facade\App;

class AuthRule extends Base {

    /**
     * @desc 菜单列表
     */
    public function index() {
        $logic = App::model('AuthRule', 'logic');
        $data = $logic->index();
        $this->assign($data);

        return view();
    }

    /**
     * @desc 添加菜单
     */
    public function add() {
        $params['action'] = input('action');

        $logic = App::model('AuthRule', 'logic');
        $data = $logic->add($params);
        $this->assign($data);

        return view();
    }

    /**
     * @desc 编辑菜单
     */
    public function edit() {
        $params['action'] = input('action');
        $params['id']     = input('id');

        $logic = App::model('AuthRule', 'logic');
        $data = $logic->edit($params);
        $this->assign($data);

        return view();
    }

    /**
     * @desc 菜单编辑 (添加、编辑)
     */
    public function operateAjax() {
        $params['action']         = input('action');
        $params['id']             = input('id', 0);
        $params['parentId']       = input('parentId');
        $params['name']           = input('name');
        $params['controllerName'] = input('controllerName');
        $params['actionName']     = input('actionName');
        $params['status']         = input('status');
        $params['icon']           = input('icon');

        $logic = model('AuthRule', 'logic');
        $data = [];
        try {
            $data = $logic->operateAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }


    /**
     * @desc 更新权限列表排序
     */
    public function updateSort() {
        $params['id']   = input('id', 0);
        $params['sort'] = input('sort');

        $logic = model('AuthRule', 'logic');
        $data = [];
        try {
            $data = $logic->updateSort($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
}
